<?php
namespace frontend\controllers;

use frontend\models\Information;
use common\components\FrontBaseController;
use frontend\models\InformationClass;
use Yii;
use \common\helps\tools;
use \common\helps\code;
use yii\db\Query;


/**
 * Information controller
 */
class InformationController extends FrontBaseController
{

    public $modelClass = 'frontend\models\Information';
    public function init()
    {
        parent::init();
    }


    /**
     * 获取资讯列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetInformationList(){
        $request =$this->request;
        $model = new Information();
        $params = $request->queryParams;
        $total_data = $model->getInformationList($params);
        $data = tools::getPageData($total_data,empty($params['page_size'])?'':$params['page_size'],empty($params['page'])?'':$params['page']);

        return code::success($data);

    }

    /**
     * 获取首页资讯列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetIndexInformationList(){
        $request =$this->request;
        $model = new Information();
        $params = $request->queryParams;
        $total_data = $model->getIndexInformationList($params);

        return code::success($total_data);

    }




    /**
     * 编辑资讯
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionEditInformation(){
        if($this->request->isPost) {
            $admin_model = new Information();
            $params = $this->request->bodyParams;
            $params['user_id'] = $this->user_id;
            if(!empty($params['type']) && $params['type']==2){ //编辑
                $admin_model->scenario='default_update';
            }else{
                return code::error('','参数错误');
            }
            $admin_model->load($params,'');
            if(!$admin_model->validate()){
                $error = array_values($admin_model->getErrors());
                return code::error('',$error[0][0]);
            }
            $data=$admin_model->editInformation($params);
            if(!empty($data['error'])){
                return code::error('',$data['error']);
            }else{
                return code::success();
            }
        }
    }




    /**
 * 获取资讯详情
 * @author hey_heyang(635870453@qq.com)
 * @return json
 */
    public  function  actionGetInformationInfo(){
        $request =$this->request;
        $params = $request->bodyParams;
        if(empty($params['id'])) return code::error('error','参数错误');
        $model = new Information();
        $data = $model->getInformationinfo($params);
        if(!empty($data)){
            return code::success($data);
        }else{
            return code::error('error','未查询到相关数据');
        }

    }

    /**
     * 获取资讯分类列表
     * @author hey_heyang(635870453@qq.com)
     * @return json
     */
    public  function  actionGetInformationClassList(){
        $request =$this->request;
        $params = $request->queryParams;
        $model = new InformationClass();
        $total_data = $model->getInformationClassList($params);
        $data = tools::getPageData($total_data, empty($params['page_size']) ? '' : $params['page_size'], empty($params['page']) ? '' : $params['page']);
        if(!empty($data)){
            return code::success($data);
        }else{
            return code::error('error','未查询到相关数据');
        }

    }



}
